package service

import (
	"github.com/spf13/viper"
	"gopkg.in/gomail.v2"
	"log"
)

func HandleEmailMessage(body []byte) {
	log.Printf("处理邮件任务: %s", body)
	to := []string{"3046305721@qq.com"}
	err := SendMail(to, "Monchi的来信", string(body))
	if err != nil {
		return
	}
}

func SendMail(to []string, subject, body string) error {

	email := viper.GetString("email.email")
	password := viper.GetString("email.password")
	m := gomail.NewMessage()
	m.SetHeader("From", email)
	m.SetHeader("To", to...)
	m.SetHeader("Subject", subject)
	m.SetBody("text/html", body)

	d := gomail.NewDialer("smtp.qq.com", 587, email, password)
	return d.DialAndSend(m)
}
